package net.caixiaomi.info.ui.discovery;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import net.caixiaomi.info.R;
import net.caixiaomi.info.base.BaseFragment;
import net.caixiaomi.info.base.quickadapter.BaseMultiItemQuickAdapter;
import net.caixiaomi.info.base.quickadapter.BaseQuickAdapter;
import net.caixiaomi.info.base.quickadapter.BaseViewHolder;
import net.caixiaomi.info.base.quickadapter.entity.MultiItemEntity;
import net.caixiaomi.info.interfaces.ComonFragmentImpl;
import net.caixiaomi.info.model.LeagueDetailMatchScorerEntity;
import net.caixiaomi.info.model.LeagueDetailScoreListEntity;
import net.caixiaomi.info.model.LeagueDetailShooterInfo;
import net.caixiaomi.info.model.LeagueDetailTeamScorer;
import net.caixiaomi.info.util.Constants;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by mac on 2018/9/12.
 */

public class LeagueScoreFragment extends BaseFragment implements ComonFragmentImpl,View.OnClickListener{

    @BindView(android.R.id.list)
    RecyclerView mListView;

    @BindView(R.id.no_data_tv)
    TextView mNoData;


    private View headerView;
    TextView lAll;
    TextView lHost;
    TextView lVisitor;

    private LeagueScoreAdapter mAdapter;

    private LeagueDetailScoreListEntity entity;

    private int leagueType; //0杯赛 1联赛

    private int mScoreType; //0:总积分 1：主场积分 2：客场积分

    @Override
    public void setType(int type) {

    }

    @Override
    public void setFilter(String filter) {

    }

    @Override
    protected int fragmentLayout() {
        return R.layout.league_shooter_layout;
    }

    @Override
    protected void onFragmentCreated(View view, @Nullable Bundle savedInstanceState) {
        mListView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mAdapter = new LeagueScoreAdapter(null);
        mListView.setAdapter(mAdapter);

    }

    public void initData(LeagueDetailScoreListEntity entity){
        this.entity=entity;
        leagueType=entity.getMatchType();
        List<LeagueDetailMatchScorerEntity> leagueShooterInfoList=entity.getMatchScoreDTOList();
        if(leagueShooterInfoList.size()==0){
            mNoData.setVisibility(View.VISIBLE);
            mListView.setVisibility(View.GONE);
            return;
        }
        if(leagueType==1){
            mScoreType=0;
            headerView=View.inflate(getActivity(),R.layout.league_score_layout,null);
            mAdapter.addHeaderView(headerView);
            notifyData();

            lAll=headerView.findViewById(R.id.score_type_all);
            lHost=headerView.findViewById(R.id.score_type_host);
            lVisitor=headerView.findViewById(R.id.score_type_visitor);
            lAll.setOnClickListener(this);
            lHost.setOnClickListener(this);
            lVisitor.setOnClickListener(this);
        }else{
            List<MultiItemEntity>list=new ArrayList<>();
            for(int i=0;i<leagueShooterInfoList.size();i++){
                List<LeagueDetailTeamScorer> leagueCcoreList=leagueShooterInfoList.get(i).getLeagueCcoreList();
                LeagueDetailTeamScorer head=new LeagueDetailTeamScorer();
                head.setType(Constants.ITEM_MATCH_LIVE1);
                list.add(head);
                head.setTeamName(leagueShooterInfoList.get(i).getGroupName());
                list.addAll(leagueCcoreList);
            }
            mAdapter.setNewData(list);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.score_type_all:
                clearTitle();
                lAll.setTextColor(getResources().getColor(R.color.white));
                lAll.setBackgroundResource(R.color.orange_primary);
                mScoreType=0;
                notifyData();
                break;
            case R.id.score_type_host:
                clearTitle();
                lHost.setTextColor(getResources().getColor(R.color.white));
                lHost.setBackgroundResource(R.color.orange_primary);
                mScoreType=1;
                notifyData();
                break;
            case R.id.score_type_visitor:
                clearTitle();
                lVisitor.setTextColor(getResources().getColor(R.color.white));
                lVisitor.setBackgroundResource(R.color.orange_primary);
                mScoreType=2;
                notifyData();
                break;
        }
    }

    private void notifyData(){
        List<LeagueDetailMatchScorerEntity> leagueShooterInfoList=entity.getMatchScoreDTOList();
        List<LeagueDetailTeamScorer> leagueCcoreList=leagueShooterInfoList.get(mScoreType).getLeagueCcoreList();
        List<MultiItemEntity>list=new ArrayList<>();
        LeagueDetailTeamScorer head=new LeagueDetailTeamScorer();
        head.setType(Constants.ITEM_MATCH_LIVE1);
        list.add(head);
        switch (mScoreType){
            case 0:
                head.setTeamName("总积分");
                break;
            case 1:
                head.setTeamName("主场积分");
                break;
            case 2:
                head.setTeamName("客场积分");
                break;
        }

        list.addAll(leagueCcoreList);
        mAdapter.setNewData(list);
    }

    private void clearTitle(){
        lAll.setTextColor(getResources().getColor(R.color.secondary_text));
        lHost.setTextColor(getResources().getColor(R.color.secondary_text));
        lVisitor.setTextColor(getResources().getColor(R.color.secondary_text));
        lAll.setBackgroundResource(R.color.white);
        lHost.setBackgroundResource(R.color.white);
        lVisitor.setBackgroundResource(R.color.white);
    }

    public class LeagueScoreAdapter extends BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder> {

        public LeagueScoreAdapter(@Nullable List<MultiItemEntity> data) {
            super(data);
            addItemType(Constants.ITEM_MATCH_LIVE0, R.layout.league_score_item_layout);
            addItemType(Constants.ITEM_MATCH_LIVE1, R.layout.league_score_item_title_layout);
        }

        @Override
        protected void convert(BaseViewHolder helper, MultiItemEntity item) {
            try {
                switch (item.getItemType()) {
                    case Constants.ITEM_MATCH_LIVE0:
                        bintData(helper, item);
                        break;
                    case Constants.ITEM_MATCH_LIVE1:
                        bintHeader(helper, item);
                        break;
                }
            }catch (Exception e){

            }
        }

        private void bintData(BaseViewHolder helper, MultiItemEntity item){
            LeagueDetailTeamScorer entity= (LeagueDetailTeamScorer) item;
            helper.setText(R.id.score_tv0,entity.getTeamOrder()+"");
            helper.setText(R.id.score_tv1,entity.getTeamName()+"");
            helper.setText(R.id.score_tv2,entity.getMatchNum()+"");
            helper.setText(R.id.score_tv3,entity.getMatchH()+"");
            helper.setText(R.id.score_tv4,entity.getMatchD()+"");
            helper.setText(R.id.score_tv5,entity.getMatchL()+"");
            helper.setText(R.id.score_tv6,entity.getBallIn()+"");
            helper.setText(R.id.score_tv7,entity.getBallLose()+"");
            helper.setText(R.id.score_tv8,entity.getScore()+"");
        }

        private void bintHeader(BaseViewHolder helper, MultiItemEntity item){
            LeagueDetailTeamScorer head= (LeagueDetailTeamScorer) item;
            helper.setText(R.id.score_title_name,head.getTeamName());
            if(leagueType==0){
                helper.setGone(R.id.score_title_name_group,true);
            }else{
                helper.setGone(R.id.score_title_name_group,false);
            }
        }
    }

}
